package com.example.stroebackstage.service;
import com.example.stroebackstage.dao.AdministratorDao;
import com.example.stroebackstage.entity.Administrator;
import com.example.stroebackstage.exception.AdminException;
import com.example.stroebackstage.exception.LoginException;
import java.util.ArrayList;

public class AdminService {
    public Administrator login(String name,String password) throws LoginException {
        Administrator administrator = AdministratorDao.selectAdmin(name,password);
        if (administrator != null){
            return administrator;
        }else {
            throw  new LoginException("用户名或密码错误");
        }
    }
    public ArrayList<Administrator> selectAll(){
        return AdministratorDao.selectAdmin();
    }
    public ArrayList<Administrator> selectById(int id){
        return AdministratorDao.selectAdmin(id);
    }
    public ArrayList<Administrator> selectByName(String name){
        return AdministratorDao.selectAdmin(name);
    }
    public boolean del(int id){return AdministratorDao.delAdmin(id);}
    public boolean updateName(int id,String name) throws AdminException {
        if (AdministratorDao.checkName(name)){
            throw new AdminException("用户名已存在");
        }
        return AdministratorDao.updateAdmin(id, name);}
    public boolean updateRole(int id,int roleId){return AdministratorDao.updateAdmin(id,roleId);}
    public boolean insert(Administrator administrator) throws AdminException {
        if (AdministratorDao.checkName(administrator.getAdminName())){
            throw new AdminException("用户名已存在");
        }
        return AdministratorDao.insertAdmin(administrator);}
}
